﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows;
using System.Windows.Data;
using WPFDiagramming.Interfaces;
using System.Windows.Documents;
using WPFDiagramming.Adorners;
using WPFDiagramming.Designer;

namespace WPFDiagramming.Controls
{
    public class Entity : ControlBase, IDraggable, IResizable
    {
        public Connector Connector { get; set; }

        static Entity()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Entity), new FrameworkPropertyMetadata(typeof(Entity)));
        }

        public Entity()
        {
            this.Background = new LinearGradientBrush(Colors.LightGray, Colors.LightYellow, 90);
        }

        protected override Geometry DrawGeometry()
        {
            if (this.MinHeight > this.Height)
                this.Height = this.MinHeight;

            this.Designer = new EntityDesigner(this.Width, this.Height);
            
            return this.Designer.Draw();
        }

        protected override void OnSelected(object sender, RoutedEventArgs e)
        {
            UIElement source = sender as UIElement;
            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(source);
            adornerLayer.Add(new ResizeAdorner(source as IResizable));
            adornerLayer.Add(new DragAndDropAdorner(source as IDraggable));
        }

        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            this.RemoveAdorners();

            base.OnMouseDoubleClick(e);
        }

        #region IPosition members

        public double Top
        {
            get
            {
                return (double)this.GetValue(Canvas.TopProperty);
            }
            set
            {
                this.SetValue(Canvas.TopProperty, value);
            }
        }

        public double Left
        {
            get
            {
                return (double)this.GetValue(Canvas.LeftProperty);
            }
            set
            {
                this.SetValue(Canvas.LeftProperty, value);
            }
        }

        public double Right
        {
            get
            {
                return (double)this.GetValue(Canvas.LeftProperty) + this.Width;
            }
        }

        public double Bottom
        {
            get
            {
                return (double)this.GetValue(Canvas.TopProperty) + this.Height;
            }
        }

        #endregion
    }
}
